package net.xdclass.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @description
 * @since 2023-07-02 21:44
 */
@Configuration
@Slf4j
@Data
public class RabbitMQConfig {

    // -------------------------订单状态检查MQ----------------------------------

    private String payEventExchange = "pay.event.exchange";

    @Bean
    public Exchange payEventExchange() {
        return new TopicExchange(payEventExchange, true, false);
    }

    private String payOrderStatusCheckQueue = "pay.order.status.check.queue";

    private String payOrderStatusRoutingKey = "pay.order.status.routing.key";

    private String payOrderStatusBindKey = "pay.order.*.routing.key";

    /**
     * 订单查询死信队列
     */
    private String payOrderStatusDeadQueue = "pay.order.status.dead.queue";

    private String payOrderStatusDeadBindKey = "pay.order.*.dead.routing.key";

    private String payOrderStatusDeadRoutingKey = "pay.order.status.dead.routing.key";

    private Integer ttl = 1000 * 60;

    @Bean
    public Queue payOrderStatusCheckQueue() {
        Map<String, Object> arguments = new HashMap<>();

        arguments.put("x-dead-letter-exchange", payEventExchange);
        arguments.put("x-dead-letter-routing-key", payOrderStatusDeadRoutingKey);
        arguments.put("x-message-ttl", ttl);
        return new Queue(payOrderStatusCheckQueue, true, false, false, arguments);
    }

    @Bean
    public Binding payOrderStatusBinding() {
        return new Binding(payOrderStatusCheckQueue, Binding.DestinationType.QUEUE,
                payEventExchange,
                payOrderStatusBindKey, null);
    }


    @Bean
    public Queue payOrderStatusDeadQueue() {
        return new Queue(payOrderStatusDeadQueue, true, false, false);
    }

    @Bean
    public Binding payOrderStatusDeadBinding() {
        return new Binding(payOrderStatusDeadQueue, Binding.DestinationType.QUEUE,
                payEventExchange,
                payOrderStatusDeadBindKey, null);
    }

    // -------------------------支付结果通知MQ----------------------------------

    // 支付结果通知队列
    private String payResultNotifyQueue = "pay.result.notify.queue";
    // 绑定key
    private String payResultNotifyBindKey = "pay.result.*.queue.routing.key";
    // 路由 key
    private String payResultNotifyRoutingKey = "pay.result.notify.queue.routing.key";

    @Bean
    public Queue payResultNotifyQueue() {
        return new Queue(payResultNotifyQueue, true, false, false);
    }

    @Bean
    public Binding payResultNotifyBinding() {
        return new Binding(payResultNotifyQueue, Binding.DestinationType.QUEUE,
                payEventExchange,
                payResultNotifyBindKey, null);
    }


}
